{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Objective : 11. Pandas for Computation\n",
    "\n",
    "<hr>\n",
    "\n",
    "1. Percent change\n",
    "2. Covariance\n",
    "3. Correlation\n",
    "4. Data Ranking\n",
    "5. Window Functions\n",
    "6. Time aware rolling\n",
    "7. Window Function\n",
    "8. Rolling vs Expanding\n",
    "\n",
    "<hr>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Statistical Functions\n",
    "\n",
    "1. Percent Change - Series and DataFrame have a method pct_change() to compute the percent change over a given number of periods "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Tea</th>\n",
       "      <th>Milk</th>\n",
       "      <th>Carpet</th>\n",
       "      <th>Cream</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2011-01</th>\n",
       "      <td>67</td>\n",
       "      <td>2</td>\n",
       "      <td>72</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-02</th>\n",
       "      <td>59</td>\n",
       "      <td>48</td>\n",
       "      <td>21</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-03</th>\n",
       "      <td>46</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-04</th>\n",
       "      <td>43</td>\n",
       "      <td>21</td>\n",
       "      <td>83</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-05</th>\n",
       "      <td>49</td>\n",
       "      <td>85</td>\n",
       "      <td>63</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-06</th>\n",
       "      <td>64</td>\n",
       "      <td>3</td>\n",
       "      <td>52</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-07</th>\n",
       "      <td>72</td>\n",
       "      <td>8</td>\n",
       "      <td>94</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-08</th>\n",
       "      <td>85</td>\n",
       "      <td>86</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-09</th>\n",
       "      <td>64</td>\n",
       "      <td>8</td>\n",
       "      <td>80</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-10</th>\n",
       "      <td>23</td>\n",
       "      <td>34</td>\n",
       "      <td>92</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Tea  Milk  Carpet  Cream\n",
       "2011-01   67     2      72     69\n",
       "2011-02   59    48      21     79\n",
       "2011-03   46    65      49     72\n",
       "2011-04   43    21      83     35\n",
       "2011-05   49    85      63     20\n",
       "2011-06   64     3      52     11\n",
       "2011-07   72     8      94     73\n",
       "2011-08   85    86       2     28\n",
       "2011-09   64     8      80     54\n",
       "2011-10   23    34      92     57"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales_data = pd.DataFrame(data=np.random.randint(1,100,(10,4)), \n",
    "                          columns=['Tea','Milk','Carpet','Cream'], \n",
    "                          index=pd.Series(pd.period_range('1/1/2011', freq='M', periods=10)))\n",
    "sales_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Changes in monthly sales data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Tea</th>\n",
       "      <th>Milk</th>\n",
       "      <th>Carpet</th>\n",
       "      <th>Cream</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2011-01</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-02</th>\n",
       "      <td>-11.94</td>\n",
       "      <td>2300.00</td>\n",
       "      <td>-70.83</td>\n",
       "      <td>14.49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-03</th>\n",
       "      <td>-22.03</td>\n",
       "      <td>35.42</td>\n",
       "      <td>133.33</td>\n",
       "      <td>-8.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-04</th>\n",
       "      <td>-6.52</td>\n",
       "      <td>-67.69</td>\n",
       "      <td>69.39</td>\n",
       "      <td>-51.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-05</th>\n",
       "      <td>13.95</td>\n",
       "      <td>304.76</td>\n",
       "      <td>-24.10</td>\n",
       "      <td>-42.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-06</th>\n",
       "      <td>30.61</td>\n",
       "      <td>-96.47</td>\n",
       "      <td>-17.46</td>\n",
       "      <td>-45.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-07</th>\n",
       "      <td>12.50</td>\n",
       "      <td>166.67</td>\n",
       "      <td>80.77</td>\n",
       "      <td>563.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-08</th>\n",
       "      <td>18.06</td>\n",
       "      <td>975.00</td>\n",
       "      <td>-97.87</td>\n",
       "      <td>-61.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-09</th>\n",
       "      <td>-24.71</td>\n",
       "      <td>-90.70</td>\n",
       "      <td>3900.00</td>\n",
       "      <td>92.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-10</th>\n",
       "      <td>-64.06</td>\n",
       "      <td>325.00</td>\n",
       "      <td>15.00</td>\n",
       "      <td>5.56</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Tea     Milk   Carpet   Cream\n",
       "2011-01    NaN      NaN      NaN     NaN\n",
       "2011-02 -11.94  2300.00   -70.83   14.49\n",
       "2011-03 -22.03    35.42   133.33   -8.86\n",
       "2011-04  -6.52   -67.69    69.39  -51.39\n",
       "2011-05  13.95   304.76   -24.10  -42.86\n",
       "2011-06  30.61   -96.47   -17.46  -45.00\n",
       "2011-07  12.50   166.67    80.77  563.64\n",
       "2011-08  18.06   975.00   -97.87  -61.64\n",
       "2011-09 -24.71   -90.70  3900.00   92.86\n",
       "2011-10 -64.06   325.00    15.00    5.56"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales_data.pct_change(periods=1).round(4)*100"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Covariance & Correlation\n",
    "Calculate covariance between series. Covariance is a measure of how much two random variables vary together\n",
    "<img src=\"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcR5-M7fDrZCkWZI2w9wVhlWsUvBmZoF94HGBYMs6L2kXFLlO095\">\n",
    "\n",
    "\n",
    "\n",
    "A correlation coefficient is a way to put a value to the relationship. Correlation coefficients have a value of b\n",
    "etween -1 and 1. A “0” means there is no relationship between the variables at all, while -1 or 1 means that there is a perfect negative or positive correlation\n",
    "<img src=\"https://www.statisticshowto.datasciencecentral.com/wp-content/uploads/2012/10/pearson-2-small.png\">\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randint(10,20,(10,2)), columns=['A','B'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "df.cov()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.182584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.182584</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B\n",
       "A  1.000000  0.182584\n",
       "B  0.182584  1.000000"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.corr()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Rank method produces a data ranking with ties being assigned the mean of the ranks (by default) for the group:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>17</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>14</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>14</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>17</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>15</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B\n",
       "0  17  17\n",
       "1  14  19\n",
       "2  10  13\n",
       "3  10  13\n",
       "4  16  10\n",
       "5  14  13\n",
       "6  17  10\n",
       "7  10  10\n",
       "8  15  19\n",
       "9  14  11"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>Rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>17</td>\n",
       "      <td>17</td>\n",
       "      <td>9.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>14</td>\n",
       "      <td>19</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10</td>\n",
       "      <td>13</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>13</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16</td>\n",
       "      <td>10</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>14</td>\n",
       "      <td>13</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>17</td>\n",
       "      <td>10</td>\n",
       "      <td>9.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>15</td>\n",
       "      <td>19</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>11</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B  Rank\n",
       "0  17  17   9.5\n",
       "1  14  19   5.0\n",
       "2  10  13   2.0\n",
       "3  10  13   2.0\n",
       "4  16  10   8.0\n",
       "5  14  13   5.0\n",
       "6  17  10   9.5\n",
       "7  10  10   2.0\n",
       "8  15  19   7.0\n",
       "9  14  11   5.0"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Rank'] = df.A.rank()\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Window Functions\n",
    "1. For working with data, a number of window functions are provided for computing common window or rolling statistics.\n",
    "2. Among these are count, sum, mean, median, correlation, variance, covariance, standard deviation, skewness, and kurtosis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "sales_data = pd.read_csv('../Data/sales-data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Month</th>\n",
       "      <th>Sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1-01</td>\n",
       "      <td>266.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1-02</td>\n",
       "      <td>145.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1-03</td>\n",
       "      <td>183.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1-04</td>\n",
       "      <td>119.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1-05</td>\n",
       "      <td>180.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1-06</td>\n",
       "      <td>168.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1-07</td>\n",
       "      <td>231.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1-08</td>\n",
       "      <td>224.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1-09</td>\n",
       "      <td>192.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1-10</td>\n",
       "      <td>122.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1-11</td>\n",
       "      <td>336.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1-12</td>\n",
       "      <td>185.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>2-01</td>\n",
       "      <td>194.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2-02</td>\n",
       "      <td>149.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>2-03</td>\n",
       "      <td>210.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>2-04</td>\n",
       "      <td>273.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>2-05</td>\n",
       "      <td>191.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>2-06</td>\n",
       "      <td>287.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2-07</td>\n",
       "      <td>226.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>2-08</td>\n",
       "      <td>303.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2-09</td>\n",
       "      <td>289.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2-10</td>\n",
       "      <td>421.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>2-11</td>\n",
       "      <td>264.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2-12</td>\n",
       "      <td>342.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>3-01</td>\n",
       "      <td>339.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>3-02</td>\n",
       "      <td>440.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>3-03</td>\n",
       "      <td>315.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>3-04</td>\n",
       "      <td>439.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>3-05</td>\n",
       "      <td>401.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>3-06</td>\n",
       "      <td>437.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>3-07</td>\n",
       "      <td>575.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>3-08</td>\n",
       "      <td>407.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>3-09</td>\n",
       "      <td>682.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>3-10</td>\n",
       "      <td>475.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>3-11</td>\n",
       "      <td>581.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>3-12</td>\n",
       "      <td>646.9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Month  Sales\n",
       "0   1-01  266.0\n",
       "1   1-02  145.9\n",
       "2   1-03  183.1\n",
       "3   1-04  119.3\n",
       "4   1-05  180.3\n",
       "5   1-06  168.5\n",
       "6   1-07  231.8\n",
       "7   1-08  224.5\n",
       "8   1-09  192.8\n",
       "9   1-10  122.9\n",
       "10  1-11  336.5\n",
       "11  1-12  185.9\n",
       "12  2-01  194.3\n",
       "13  2-02  149.5\n",
       "14  2-03  210.1\n",
       "15  2-04  273.3\n",
       "16  2-05  191.4\n",
       "17  2-06  287.0\n",
       "18  2-07  226.0\n",
       "19  2-08  303.6\n",
       "20  2-09  289.9\n",
       "21  2-10  421.6\n",
       "22  2-11  264.5\n",
       "23  2-12  342.3\n",
       "24  3-01  339.7\n",
       "25  3-02  440.4\n",
       "26  3-03  315.9\n",
       "27  3-04  439.3\n",
       "28  3-05  401.3\n",
       "29  3-06  437.4\n",
       "30  3-07  575.5\n",
       "31  3-08  407.6\n",
       "32  3-09  682.0\n",
       "33  3-10  475.3\n",
       "34  3-11  581.3\n",
       "35  3-12  646.9"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = sales_data.Sales.rolling(window=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     1.0\n",
       "1     2.0\n",
       "2     3.0\n",
       "3     4.0\n",
       "4     5.0\n",
       "5     5.0\n",
       "6     5.0\n",
       "7     5.0\n",
       "8     5.0\n",
       "9     5.0\n",
       "10    5.0\n",
       "11    5.0\n",
       "12    5.0\n",
       "13    5.0\n",
       "14    5.0\n",
       "15    5.0\n",
       "16    5.0\n",
       "17    5.0\n",
       "18    5.0\n",
       "19    5.0\n",
       "20    5.0\n",
       "21    5.0\n",
       "22    5.0\n",
       "23    5.0\n",
       "24    5.0\n",
       "25    5.0\n",
       "26    5.0\n",
       "27    5.0\n",
       "28    5.0\n",
       "29    5.0\n",
       "30    5.0\n",
       "31    5.0\n",
       "32    5.0\n",
       "33    5.0\n",
       "34    5.0\n",
       "35    5.0\n",
       "Name: Sales, dtype: float64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       NaN\n",
       "1       NaN\n",
       "2       NaN\n",
       "3       NaN\n",
       "4     266.0\n",
       "5     183.1\n",
       "6     231.8\n",
       "7     231.8\n",
       "8     231.8\n",
       "9     231.8\n",
       "10    336.5\n",
       "11    336.5\n",
       "12    336.5\n",
       "13    336.5\n",
       "14    336.5\n",
       "15    273.3\n",
       "16    273.3\n",
       "17    287.0\n",
       "18    287.0\n",
       "19    303.6\n",
       "20    303.6\n",
       "21    421.6\n",
       "22    421.6\n",
       "23    421.6\n",
       "24    421.6\n",
       "25    440.4\n",
       "26    440.4\n",
       "27    440.4\n",
       "28    440.4\n",
       "29    440.4\n",
       "30    575.5\n",
       "31    575.5\n",
       "32    682.0\n",
       "33    682.0\n",
       "34    682.0\n",
       "35    682.0\n",
       "Name: Sales, dtype: float64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.max()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Time aware rolling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "dft = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]},\n",
    "                    index=pd.date_range('20130101 09:00:00',\n",
    "                                        periods=5,\n",
    "                                        freq='s'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01 09:00:00</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 09:00:01</th>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 09:00:02</th>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 09:00:03</th>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 09:00:04</th>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       B\n",
       "2013-01-01 09:00:00  0.0\n",
       "2013-01-01 09:00:01  1.0\n",
       "2013-01-01 09:00:02  3.0\n",
       "2013-01-01 09:00:03  2.0\n",
       "2013-01-01 09:00:04  4.0"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft.rolling('2s').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        NaN\n",
       "1        NaN\n",
       "2        NaN\n",
       "3        NaN\n",
       "4      894.6\n",
       "5      797.1\n",
       "6      883.0\n",
       "7      924.4\n",
       "8      997.9\n",
       "9      940.5\n",
       "10    1108.5\n",
       "11    1062.6\n",
       "12    1032.4\n",
       "13     989.1\n",
       "14    1076.3\n",
       "15    1013.1\n",
       "16    1018.6\n",
       "17    1111.3\n",
       "18    1187.8\n",
       "19    1281.3\n",
       "20    1297.9\n",
       "21    1528.1\n",
       "22    1505.6\n",
       "23    1621.9\n",
       "24    1658.0\n",
       "25    1808.5\n",
       "26    1702.8\n",
       "27    1877.6\n",
       "28    1936.6\n",
       "29    2034.3\n",
       "30    2169.4\n",
       "31    2261.1\n",
       "32    2503.8\n",
       "33    2577.8\n",
       "34    2721.7\n",
       "35    2793.1\n",
       "Name: Sales, dtype: float64"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.agg(np.sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>mean</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>894.6</td>\n",
       "      <td>178.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>797.1</td>\n",
       "      <td>159.42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>883.0</td>\n",
       "      <td>176.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>924.4</td>\n",
       "      <td>184.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>997.9</td>\n",
       "      <td>199.58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>940.5</td>\n",
       "      <td>188.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1108.5</td>\n",
       "      <td>221.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1062.6</td>\n",
       "      <td>212.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1032.4</td>\n",
       "      <td>206.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>989.1</td>\n",
       "      <td>197.82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1076.3</td>\n",
       "      <td>215.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1013.1</td>\n",
       "      <td>202.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1018.6</td>\n",
       "      <td>203.72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1111.3</td>\n",
       "      <td>222.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1187.8</td>\n",
       "      <td>237.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1281.3</td>\n",
       "      <td>256.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>1297.9</td>\n",
       "      <td>259.58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1528.1</td>\n",
       "      <td>305.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1505.6</td>\n",
       "      <td>301.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>1621.9</td>\n",
       "      <td>324.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>1658.0</td>\n",
       "      <td>331.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1808.5</td>\n",
       "      <td>361.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1702.8</td>\n",
       "      <td>340.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1877.6</td>\n",
       "      <td>375.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>1936.6</td>\n",
       "      <td>387.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>2034.3</td>\n",
       "      <td>406.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>2169.4</td>\n",
       "      <td>433.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>2261.1</td>\n",
       "      <td>452.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>2503.8</td>\n",
       "      <td>500.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>2577.8</td>\n",
       "      <td>515.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>2721.7</td>\n",
       "      <td>544.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>2793.1</td>\n",
       "      <td>558.62</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       sum    mean\n",
       "0      NaN     NaN\n",
       "1      NaN     NaN\n",
       "2      NaN     NaN\n",
       "3      NaN     NaN\n",
       "4    894.6  178.92\n",
       "5    797.1  159.42\n",
       "6    883.0  176.60\n",
       "7    924.4  184.88\n",
       "8    997.9  199.58\n",
       "9    940.5  188.10\n",
       "10  1108.5  221.70\n",
       "11  1062.6  212.52\n",
       "12  1032.4  206.48\n",
       "13   989.1  197.82\n",
       "14  1076.3  215.26\n",
       "15  1013.1  202.62\n",
       "16  1018.6  203.72\n",
       "17  1111.3  222.26\n",
       "18  1187.8  237.56\n",
       "19  1281.3  256.26\n",
       "20  1297.9  259.58\n",
       "21  1528.1  305.62\n",
       "22  1505.6  301.12\n",
       "23  1621.9  324.38\n",
       "24  1658.0  331.60\n",
       "25  1808.5  361.70\n",
       "26  1702.8  340.56\n",
       "27  1877.6  375.52\n",
       "28  1936.6  387.32\n",
       "29  2034.3  406.86\n",
       "30  2169.4  433.88\n",
       "31  2261.1  452.22\n",
       "32  2503.8  500.76\n",
       "33  2577.8  515.56\n",
       "34  2721.7  544.34\n",
       "35  2793.1  558.62"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.agg([np.sum, np.mean])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Rolling vs Expanding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.DataFrame([\n",
    "    ['a', 1],\n",
    "    ['a', 2],\n",
    "    ['a', 3],\n",
    "    ['b', 5],\n",
    "    ['b', 6],\n",
    "    ['b', 7],\n",
    "    ['b', 8],\n",
    "    ['c', 10],\n",
    "    ['c', 11],\n",
    "    ['c', 12],\n",
    "    ['c', 13]\n",
    "], columns = ['category', 'value'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      1.0\n",
       "1      3.0\n",
       "2      6.0\n",
       "3     11.0\n",
       "4     17.0\n",
       "5     24.0\n",
       "6     32.0\n",
       "7     42.0\n",
       "8     53.0\n",
       "9     65.0\n",
       "10    78.0\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.value.expanding(1).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      NaN\n",
       "1      3.0\n",
       "2      5.0\n",
       "3      8.0\n",
       "4     11.0\n",
       "5     13.0\n",
       "6     15.0\n",
       "7     18.0\n",
       "8     21.0\n",
       "9     23.0\n",
       "10    25.0\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.value.rolling(2).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Expanding - If we use the expanding window with initial size 1, it will create a window that in the first step contains only the first row. In the second step, it contains both the first and the second row. In every step, one additional row is added to the window, and the aggregating function is being recalculated.\n",
    "\n",
    "2. Rolling - Rolling windows are totally different. In this case, we specify the size of the window which is moving. What happens when we set the rolling window size to 2?\n",
    "\n",
    "   - In the first step, it is going to contain the first row and one undefined row, so I am going to get NaN as a result.\n",
    "\n",
    "   - In the second step, the window moves and now contains the first and the second row. Now it is possible to calculate the aggregate function. In the case of this example, the sum of both rows.\n",
    "\n",
    "   - In the third step, the window moves again and no longer contains the first row. Instead of that now it calculates the sum of the second and the third row."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
